<?php

/**
 * wiki actions.
 *
 * @package    ser
 * @subpackage wiki
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class wikiActions extends sfActions
{
    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $r)
    {
		$this->redirect('@wiki_show?pr_slug='.$r->getParameter('pr_slug').'&name=Main_Page');
    }

    public function executeShow(sfWebRequest $r)
    {
        $this->wikiPage = $this->getPage($r);

        if (!$this->wikiPage)
            $this->forward('wiki', 'notFound');
    }

    public function executeNotFound(sfWebRequest $request)
    {
        $pageName = $request->getParameter('name');

	    $this->wikiPage = new WikiPage();
		$this->wikiPage->setName($pageName);
		$this->projectName = $request->getParameter('pr_slug');
	}

	public function executeNew(sfWebRequest $r)
	{
	    $pr_slug = $r->getParameter('pr_slug');
		$projectName = SER::deSlugify($pr_slug);
		
		$pageName = $r->getParameter('name');
		
		$this->form = new NewWikiPageForm();
		
		if ($r->isMethod('put'))
		{
			$this->form->bind($r->getParameter('wikiPage'));
		
			if ($this->form->isValid())
			{				
				$newPage = new WikiPage();
				$newPage->setName($pageName);
				$newPage->setProject(Doctrine::getTable('Project')->findOneByName($projectName));
				
				$newRev = new WikiRevision();
				$newRev->setContent($this->form->getValue('content'));
				$newRev->setComment($this->form->getValue('comment'));
				$newRev->setWikiPage($newPage);
				$newRev->setUserId($this->getUser()->getID());
				
				$newPage->save();
				$newRev->save();
				
				$this->redirect('@wiki_show?name='.$pageName.'&pr_slug='.$pr_slug);
			}
		}
		
		$this->wikiPage = Doctrine::getTable('WikiPage')->getPage($projectName, $pageName);
		if ($this->wikiPage)
		{
		    $this->redirect('@wiki_show?name='.$pageName.'&pr_slug='.$pr_slug);
		}
		
		$this->wikiPage = new WikiPage();
		$this->wikiPage->setName($pageName);
		$this->wikiPage->setProject(Doctrine::getTable('Project')->findOneByName($projectName));
		
		$this->form->setDefaults( array ('content'=>'Content for '.$this->wikiPage->getRName(), 'comment'=>'Creating page: '.$this->wikiPage->getRName()));
	}

	public function executeEdit(sfWebRequest $r)
	{
	    $pr_slug = $r->getParameter('pr_slug');		
		$projectName = SER::deSlugify($pr_slug);
		
		$pageName = $r->getParameter('name');
		
		$this->wikiPage = Doctrine::getTable('WikiPage')->getPage($projectName, $pageName);
		if (!$this->wikiPage)
		{
		    $this->redirect('@wiki_new?name='.$pageName.'&pr_slug='.$pr_slug);
		}
		
		$this->form = new NewWikiPageForm();
		
		if ($r->isMethod('put'))
		{
			$this->form->bind($r->getParameter('wikiPage'));
		
			if ($this->form->isValid())
			{
				$newRev = new WikiRevision();
				$newRev->setContent($this->form->getValue('content'));
				$newRev->setComment($this->form->getValue('comment'));
				$newRev->setWikiPage($this->wikiPage);
				$newRev->setUserId($this->getUser()->getID());
				
				$newRev->save();
				
				$this->redirect('@wiki_show?name='.$pageName.'&pr_slug='.$pr_slug);
			}
		}
		$this->form->setDefault('content', $this->wikiPage->getLatestRevision()->getContent());
	}
	
	public function executeShowRevs(sfWebRequest $r)
	{
		$this->wikiPage = $this->getPage($r);
		
		$this->pager = new sfDoctrinePager(
				'WikiRevision',
				sfConfig::get('app_max_revs_per_page')
		);
		$this->pager->setQuery($this->wikiPage->getRevisionsQuery());
		$this->pager->setPage($r->getParameter('page', 1));
		$this->pager->init();
	}
	
	public function executeShowRev(sfWebRequest $r)
	{
		$this->wikiPage = $this->getPage($r);
		$this->revision = Doctrine::getTable('WikiRevision')->find($r->getParameter('id'));
		
		if (!$this->revision)
			$this->redirect('@wiki_show_revs?pr_slug='.$wikiPage->getPRSlug().'&name='.$wikiPage->getName());
	}
	
	public function executeManage(sfWebRequest $r)
	{
		$this->wikiPage = $this->getPage($r);
	}
	
	private function getPage(sfWebRequest $r)
	{
		$projectName = SER::deSlugify($r->getParameter('pr_slug'));
		$pageName = $r->getParameter('name');
		
		return Doctrine::getTable('WikiPage')->getPage($projectName, $pageName);
	}
}
